﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IOBusiness;
using IOEntity;
namespace Indochinaodyssey.Admin
{
    public partial class HinhAnh : System.Web.UI.Page
    {
        clsBSHinhAnh obj = new clsBSHinhAnh();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (Request.QueryString["action"] != null)
                {
                    if (this.pnInfo.Visible == false)
                    {
                        if (Request.QueryString["action"].ToString().Equals("new"))
                        {
                            this.pnInfo.Visible = true;
                            this.txtTieuDe.Text = "";
                            this.imgHinhAnh.ImageUrl = "";
                        }
                        else
                        {
                            if (Request.QueryString["action"].ToString().Equals("edit"))
                            {
                                if (Request.QueryString["ID"] != null)
                                {
                                    this.pnInfo.Visible = true;
                                    int id = Convert.ToInt32(Request.QueryString["ID"]);
                                    clsHinhAnh hinhanh = new clsHinhAnh();
                                    hinhanh = obj.GetById(id);
                                    this.txtTieuDe.Text = hinhanh.TieuDe;
                                    this.imgHinhAnh.ImageUrl = "~/Images/HinhAnh/" + hinhanh.Url;
                                    this.hfHinhAnh.Value = hinhanh.Url;
                                }
                            }
                        }
                    }
                }
                else
                {
                    this.pnInfo.Visible = false;
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["action"] != null)
                {
                    clsHinhAnh hinhanh = new clsHinhAnh();
                    hinhanh.TieuDe = this.txtTieuDe.Text;
                    hinhanh.Url = hfHinhAnh.Value;
                    hinhanh.LoaiHinhAnh= new clsLoaiHinhAnh();
                    if (Request.QueryString["loaiid"] != null && !String.IsNullOrEmpty(Request.QueryString["loaiid"]))
                    hinhanh.LoaiHinhAnh.Id = Convert.ToInt32(Request.QueryString["loaiid"].ToString());
                    hinhanh.Tour= new clsTour();
                    if (Request.QueryString["tourid"] != null && !String.IsNullOrEmpty(Request.QueryString["tourid"]))
                        hinhanh.Tour.Id = Convert.ToInt32(Request.QueryString["tourid"].ToString());
                    string filename = "";
                    if (Request.QueryString["action"].ToString().Equals("new"))
                    {
                        if (this.fuFile.HasFile)
                        {
                            filename = clsBSFileUpload.SaveFile(this.fuFile, Server.MapPath("~/Images/HinhAnh/"));
                            hinhanh.Url = filename;
                        }
                        obj.Insert(hinhanh);
                        this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "show", "<script type='text/javascript'>alert('Thêm mới thành công')</script>");
                    }
                    else
                    {
                        if (Request.QueryString["action"].ToString().Equals("edit"))
                        {
                            if (Request.QueryString["ID"] != null)
                            {
                                int id = Convert.ToInt32(Request.QueryString["ID"]);
                                hinhanh.Id = id;
                                if (this.fuFile.HasFile)
                                {
                                    clsBSFileUpload.DeleteFile(Server.MapPath("~/Images/HinhAnh/") + this.hfHinhAnh.Value);
                                    filename = clsBSFileUpload.SaveFile(this.fuFile, Server.MapPath("~/Images/HinhAnh/"));
                                    hinhanh.Url = filename;
                                }
                                obj.Update(hinhanh);
                                this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "show", "<script type='text/javascript'>alert('Cập nhật thành công')</script>");
                            }
                        }
                    }
                }
                this.odsHinhAnh.DataBind();
                this.GridView1.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("Xoa"))
                {
                    clsHinhAnh hinhanh = new clsHinhAnh();
                    hinhanh = obj.GetById(Convert.ToInt32(e.CommandArgument));
                    clsBSFileUpload.DeleteFile(Server.MapPath("~/Images/HinhAnh/") + hinhanh.Url);
                    obj.Delete(Convert.ToInt32(e.CommandArgument));
                    this.GridView1.DataBind();
                }
                else
                {
                    if (e.CommandName.Equals("sua"))
                    {
                        Response.Redirect("HinhAnh.aspx?action=edit&id=" + e.CommandArgument + "&loaiid=" + Request.QueryString["loaiid"] + "&tourid=" + Request.QueryString["tourid"]);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("HinhAnh.aspx?action=new&loaiid=" + Request.QueryString["loaiid"] + "&tourid=" + Request.QueryString["tourid"]);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("HinhAnh.aspx?loaiid=" + Request.QueryString["loaiid"] + "&tourid=" + Request.QueryString["tourid"]);
        }
                
    }
}